﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace RenjianImageViewer.ViewModel.Command
{
    class SaveConversitionImagesCommand : RenjianCommandBase
    {
        public SaveConversitionImagesCommand(RenjianViewModel viewModel)
            : base(viewModel)
        {
            //Might cause memory leak, but commands will never be disposed before ViewModel, so ignore it.
            viewModel.ConversitionImages.CollectionChanged += (s, e) => NotifyCanExecuteChanged();
        }

        public override bool CanExecute(object parameter)
        {
            return viewModel.ConversitionImages.Count > 0;
        }

        public override void Execute(object parameter)
        {
            //var saveState = new ViewModel.SavingMultiImageStateViewModel { ShowState = true, Count = viewModel.ConversitionImages.Count };
            //BusyMask.DataContext = saveState;
            //BusyMask.Visibility = Visibility.Visible;

            //int closureImageCount = viewModel.ConversitionImages.Count;

            //for (int i = 0; i < viewModel.ConversitionImages.Count; i++)
            //{
            //    WebClient wc = new WebClient();
            //    //Hack to remove watermark
            //    wc.Headers.Add(HttpRequestHeader.Referer, "http://renjian.com/home");
            //    wc.DownloadProgressChanged += (s, e) =>
            //    {
            //        dynamic state = e.UserState;
            //        saveState.SetPerentage(state.Index, e.ProgressPercentage);
            //    };
            //    wc.DownloadFileCompleted += (s, e) =>
            //    {
            //        saveState.Position++;
            //        dynamic state = e.UserState;
            //        WebClient webClient = state.DownloadWorker;
            //        if (null != webClient)
            //        {
            //            webClient.Dispose();
            //        }
            //        if (saveState.Position == closureImageCount)
            //        {
            //            viewModel.UpdateImagesIsLocalStatus();
            //            BusyMask.Visibility = Visibility.Collapsed;
            //        }
            //    };
            //    wc.DownloadFileAsync(new Uri(viewModel.ConversitionImages[i].ImageUrl),
            //            @"c:\\rensea\" + viewModel.ConversitionImages[i].StatusID.ToString() +
            //            System.IO.Path.GetExtension(viewModel.ConversitionImages[i].ImageUrl), new { DownloadWorker = wc, Index = i });
            //}
        }
    }
}
